package cj.web.lkg.controllers;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import net.paoding.rose.web.annotation.Param;
import net.paoding.rose.web.annotation.Path;
import net.paoding.rose.web.annotation.rest.Get;
import net.paoding.rose.web.var.Model;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;

import cj.utils.Base64;
import cj.utils.Constants;
import cj.utils.DateUtils;
import cj.utils.ReturnUUID;
import cj.web.constants.ActionEnum;
import cj.web.constants.AppUserInfoStateEnum;
import cj.web.constants.NewsTypeEnum;
import cj.web.lkg.dao.AppUserDAO;
import cj.web.lkg.dao.ConfigDAO;
import cj.web.lkg.dao.NewsDAO;
import cj.web.lkg.dao.NewsOpDAO;
import cj.web.login.controllers.BaseController;

@Path("/news")
public class NewsController extends BaseController{

	private static Logger logger = Logger.getLogger(NewsController.class.getName());
	
//	private static Log log = LogFactory.getLog("railyFile");
	
	//视觉大图额栏目ID
	private static int sjColumnsId=7;
	
	@Autowired
	private NewsDAO newsDao;
	@Autowired
	private NewsOpDAO newsOpDao;
	@Autowired
	private AppUserDAO appuserDao;
	@Autowired
	private ConfigDAO configDao;
	
	/**
	 * 新闻列表数据 下拉刷新 
	 * @param columnsId
	 * @param releaseAt
	 * @return
	 * @throws Exception
	 */
	@Get("downList")
	public String downList(@Param("columnsId") int columnsId,@Param("releaseAt")String releaseAt) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(columnsId==sjColumnsId){
			List<Integer> ids = new ArrayList<Integer>();
			List<Map> news = newsDao.queryNewsByLastDate(ids, columnsId,releaseAt);//新闻列表
			news = formReturnListTop(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("list", news);
			
			int size = newsDao.querySizeByLastDate(ids, columnsId,releaseAt);
			jo.put("total", size);
		}else{
			List<Map> tops = newsDao.queryTopByColumnsId(columnsId);//获取头条
			tops = formReturnListTop(tops);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("tops", tops);
			
			List<Integer> ids = new ArrayList<Integer>();//排除头条，请求列表
			for(Map map : tops){
				ids.add(Integer.valueOf(map.get("id").toString()));
			}
			
			List<Map> news = newsDao.queryNewsByLastDate(ids, columnsId,releaseAt);//新闻列表
			news = formReturnList(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("list", news);
			
			int size = newsDao.querySizeByLastDate(ids, columnsId,releaseAt);
			jo.put("total", size);
		}
		
		
		return "@"+jo.toString();
	}
	
	/**
	 * 新闻列表数据 上拉加载
	 * @param columnsId
	 * @param releaseAt
	 * @param start
	 * @param length
	 * @param devicesId
	 * @return
	 * @throws Exception
	 */
	@Get("upList")
	public String upList(@Param("columnsId")int columnsId,@Param("releaseAt")String releaseAt, @Param("start")int start, @Param("length")int length, @Param("devicesId")String devicesId) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		if(columnsId==sjColumnsId){
			List<Integer> ids = new ArrayList<Integer>();
			List<Map> news = newsDao.queryNewsByColumnsId(ids, columnsId, start, length,releaseAt);//新闻列表
			news = formReturnListTop(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("list", news);
			
			int size = newsDao.querySizeByColumnsId(ids, columnsId,releaseAt);
			jo.put("total", size);
		}else{
			List<Map> tops = newsDao.queryTopByColumnsId(columnsId);//获取头条
			tops = formReturnList(tops);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
//			jo.put("tops", tops);
			
			List<Integer> ids = new ArrayList<Integer>();//排除头条，请求列表
			for(Map map : tops){
				ids.add(Integer.valueOf(map.get("id").toString()));
			}
			
			List<Map> news = newsDao.queryNewsByColumnsId(ids, columnsId, start, length,releaseAt);//新闻列表
			news = formReturnList(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("list", news);
			
			int size = newsDao.querySizeByColumnsId(ids, columnsId,releaseAt);
			jo.put("total", size);
		}
		
		
		return "@"+jo.toString();
	}
	
	@Get("upSSList")//搜索新闻 上拉加载
	public String upSSList(@Param("start")int start, @Param("length")int length, @Param("keyword")String keyword) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> columnls = configDao.queryColumnsAll();
		if( keyword != null && !"".equals(keyword) ){
			//keyword = Base64.getFromBase64(keyword);
			
			if(keyword.length() >= 2){
				List<Map> news = newsDao.queryNewsByKeyword(keyword,start, length);//新闻列表
				news = formReturnList(news,columnls);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
				jo.put("list", news);
				
				int size = newsDao.querySizeByKeyword(keyword);
				jo.put("total", size);
			}
			
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 图文新闻  详情数据请求
	 */
	@Get("detailTW")
	public String detailTW(@Param("id")int id,@Param("devicesId") String devicesId) throws ParseException{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> objs = newsDao.queryById(id);
		if(objs != null && objs.size() > 0){
			Map obj = objs.get(0);
			String content = "";
			if(obj.get("content")!=null && !"".equals(obj.get("content").toString()))
			{
				content = Base64.getBase64(obj.get("content").toString());
			}
			obj.put("content", content);
			
			if(obj.get("releaseAt") != null && !"".equals(obj.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(obj.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				obj.put("releaseAt", releaseAt);
			}
			
			//视频地址
			String vedioUrl = "";
			if(obj.get("vedioUrl")!=null && !"".equals(obj.get("vedioUrl").toString()))
			{
				vedioUrl = obj.get("vedioUrl").toString().equals("")?"":videoPath+obj.get("vedioUrl").toString();
			}
			obj.put("vedioUrl", vedioUrl);
//			obj.put("carouselImage", obj.get("carouselImage").toString().equals("")?"":imgPath+obj.get("carouselImage").toString());
			
			//缩率图
			String carouselImage = obj.get("carouselImage")==null?"":obj.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				obj.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
			}
			else
			{
				obj.put("carouselImage", "");//缩率图
			}
			
			
			jo.put("obj", obj);
			
			//添加新闻的浏览量
			String userName = addNewPvCount(id, devicesId);
			
			String keyWord = "";
			List<Map> relevantNews = new ArrayList<Map>();
			if(obj.get("keywords") != null){//相关阅读 列表
				keyWord = obj.get("keywords").toString();
			}
			if(!"".equals(keyWord)){
				relevantNews = newsDao.queryByKeyWord(keyWord,id);//相关阅读
				relevantNews = formReturnList(relevantNews);
			}
			jo.put("relNews", relevantNews);
			
			Map rep = returnMyOp(userName, id);
			jo.put("myOp", rep);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 图集新闻详情数据请求
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@Get("detailTJ")
	public String detailTJ(@Param("id")int id,@Param("devicesId") String devicesId) throws ParseException{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> objs = newsDao.queryById(id);
		
		if(objs != null && objs.size() > 0 ){
			Map obj = objs.get(0);
			Map returnMap = extractImgAndText(obj.get("content").toString());
			obj.put("imgs", returnMap.get("imgs"));
			obj.put("docs", returnMap.get("texts"));
			String content = Base64.getBase64(obj.get("content").toString());
			obj.put("content", content);
			
			if(obj.get("releaseAt") != null && !"".equals(obj.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(obj.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				obj.put("releaseAt", releaseAt);
			}
//			obj.put("carouselImage", obj.get("carouselImage").toString().equals("")?"":imgPath+obj.get("carouselImage").toString());
			
			//缩率图
			String carouselImage = obj.get("carouselImage")==null?"":obj.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				//obj.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
				//图片名称
				String fileName = carouselImages[1];
				//扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
				
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	obj.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
                }else{
                	obj.put("carouselImage", imgPath+carouselImages[0]+"/"+carouselImages[1]);//缩率图
                }
			}
			else
			{
				obj.put("carouselImage", "");//缩率图
			}
			
			jo.put("obj", obj);
			
			//添加新闻的浏览量
			String userName = addNewPvCount(id, devicesId);
			
			Map rep = returnMyOp(userName, id);
			jo.put("myOp", rep);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 视频新闻 详情数据请求
	 * @param id
	 * @return
	 * @throws ParseException
	 */
	@Get("detailSP")
	public String detailSP(@Param("id")int id,@Param("devicesId") String devicesId) throws ParseException{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> objs = newsDao.queryById(id);
		if(objs != null && objs.size() > 0){
			Map obj = objs.get(0);
			String content = "";
			if(obj.get("content")!=null && !"".equals(obj.get("content").toString()))
			{
				content = Base64.getBase64(obj.get("content").toString());
			}
			obj.put("content", content);
			
			if(obj.get("releaseAt") != null && !"".equals(obj.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(obj.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				obj.put("releaseAt", releaseAt);
			}
			
			//视频地址
			obj.put("vedioUrl", obj.get("vedioUrl").toString().equals("")?"":videoPath+obj.get("vedioUrl").toString());
			obj.put("carouselImage", obj.get("carouselImage").toString().equals("")?"":imgPath+obj.get("carouselImage").toString());
			jo.put("obj", obj);
			
			//添加新闻的浏览量
			String userName = addNewPvCount(id, devicesId);
			
			Map rep = returnMyOp(userName, id);
			jo.put("myOp", rep);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 投票新闻  详情数据请求
	 */
	@Get("detailTP")
	public String detailTP(@Param("id")int id,@Param("devicesId") String devicesId) throws ParseException{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		List<Map> objs = newsDao.queryById(id);
		if(objs != null && objs.size() > 0){
			Map obj = objs.get(0);
			String content = "";
			if(obj.get("content")!=null && !"".equals(obj.get("content").toString()))
			{
				content = Base64.getBase64(obj.get("content").toString());
			}
			obj.put("content", content);
			
			if(obj.get("releaseAt") != null && !"".equals(obj.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(obj.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				obj.put("releaseAt", releaseAt);
			}
			
			//视频地址
			String vedioUrl = "";
			if(obj.get("vedioUrl")!=null && !"".equals(obj.get("vedioUrl").toString()))
			{
				vedioUrl = obj.get("vedioUrl").toString().equals("")?"":videoPath+obj.get("vedioUrl").toString();
			}
			obj.put("vedioUrl", vedioUrl);
			
			//缩率图
			String carouselImage = obj.get("carouselImage")==null?"":obj.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				obj.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
			}
			else
			{
				obj.put("carouselImage", "");//缩率图
			}
			
			//投票
			if(null != obj.get("voteId") && !"".equals(obj.get("voteId").toString())){
				int voteId = Integer.parseInt(obj.get("voteId").toString());
				if(voteId > -1){
					boolean hasVoted = hasVote(voteId, devicesId);
					int overdue = overdue(voteId);
					
					List<Map> votes =  newsDao.getVoteById(voteId);
					if(null != votes && votes.size() > 0){
						Map vote = votes.get(0);
						vote.put("overdue", overdue);
						vote.put("hasVoted", hasVoted);
						
						List<Map> subjects = newsDao.getSubjByVoteId(voteId);
						
						if(null != subjects && subjects.size() > 0){
							for(Map subject : subjects){
								int subId = Integer.parseInt(subject.get("id").toString());
								List<Map> items = newsDao.getItmBySubId(subId);
								int total = 0;
								if(null != items && items.size() > 0){
									for(Map item : items){
										int count = Integer.parseInt(item.get("count").toString());
										total += count;
									}
									
									subject.put("total", total);
									subject.put("items", items);
								}
							}
							vote.put("subjects", subjects);
						}
						obj.put("vote", vote);
					}
				}
			}
			
			jo.put("obj", obj);
			
			//添加新闻的浏览量
			String userName = addNewPvCount(id, devicesId);
			
			String keyWord = "";
			List<Map> relevantNews = new ArrayList<Map>();
			if(obj.get("keywords") != null){//相关阅读 列表
				keyWord = obj.get("keywords").toString();
			}
			if(!"".equals(keyWord)){
				relevantNews = newsDao.queryByKeyWord(keyWord,id);//相关阅读
				relevantNews = formReturnList(relevantNews);
			}
			jo.put("relNews", relevantNews);
			
			Map rep = returnMyOp(userName, id);
			jo.put("myOp", rep);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@" + jo.toString();
	}
	
	private int overdue(int voteId) {
		if(newsDao.notStart(voteId) > 0){
			return 0;
		}
		else if(newsDao.during(voteId) > 0){
			return 1;
		}
		else{
			return 2;
		}
	}

	private boolean hasVote(int voteId, String devicesId) {
		int count = newsDao.voteLogCnt(voteId, devicesId);
		if(count > 0){
			return true;
		}
		return false;
	}

	@Get("vote")
	public String vote(@Param("voted")String voted, @Param("devicesId")String devicesId){
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		JSONArray jaVoted = JSONArray.fromObject(voted);
		for(int i = 0; i < jaVoted.size(); i++){
			JSONObject votedItm = jaVoted.getJSONObject(i);
			int subjectId = votedItm.getInt("subjectId");
			int itemId = votedItm.getInt("itemId");
			
			//投票数 + 1
			newsDao.addItmCnt(itemId);
			//写入log
			newsDao.crtVoteLog(subjectId, itemId, devicesId);
		}
		
		return "@" + jo.toString();
	}
	
	/**
	 * 添加新闻的浏览量
	 * @param newsId
	 * @param devicesId
	 */
	private String addNewPvCount(int newsId,String devicesId)
	{
		int userId = 0;
		String userName = devicesId;
		//app用户信息by设备ID
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			int weixinuuid = Integer.parseInt(apuser.get(0).get("weixinuuid").toString());
			int qquuid = Integer.parseInt(apuser.get(0).get("qquuid").toString());
			int weibouuid = Integer.parseInt(apuser.get(0).get("weibouuid").toString());
			int accountlogin = Integer.parseInt(apuser.get(0).get("accountlogin").toString());
			
			String wxid = apuser.get(0).get("wxid")==null?"":apuser.get(0).get("wxid").toString();
			String qqid = apuser.get(0).get("qqid")==null?"":apuser.get(0).get("qqid").toString();
			String sinawbid = apuser.get(0).get("sinawbid")==null?"":apuser.get(0).get("sinawbid").toString();
			String account = apuser.get(0).get("account")==null?"":apuser.get(0).get("account").toString();
			
			//回复用户名称修改为第三方登录ID
			if(weixinuuid==1){
				//微信登录
				userName=wxid;
			}else if(qquuid==1){
				//QQ登录
				userName=qqid;
			}else if(weibouuid==1){
				//微博登录
				userName=sinawbid;
			}else if(accountlogin==1){
				//微博登录
				userName=account;
			}
			
			//查询新闻By ID
			List<Map> news = newsDao.queryById(newsId);
			if(news.size()>0)
			{
				userId = Integer.parseInt(apuser.get(0).get("id").toString());
				int newsPvSize = newsOpDao.queryNewsPVSize(newsId, userId);
				if(newsPvSize==0)
				{
//					Map rep = new HashMap();
//					rep.put("userId", userId);
//					rep.put("userName", apuser.get(0).get("name")!=null?apuser.get(0).get("name").toString():"");
//					rep.put("newsId", newsId);
//					rep.put("newsTitle", news.get(0).get("title").toString());
//					rep.put("createUser", userId);
//					rep.put("updateUser", userId);
					//创建新闻PV数
					//newsOpDao.createNewsPV(rep);
					printUserActionLog(ActionEnum.VIEW.getValue(), userId, newsId);
					//增加PV量
					newsOpDao.addNewsPVById(newsId);
				}
			}
		}
		
		return userName;
	}
	
	/**
	 * 专题列表数据 上拉加载
	 * @param subjectId
	 * @param newestId
	 * @param columnsId
	 * @param start
	 * @param length
	 * @param devicesId
	 * @return
	 * @throws Exception
	 */
	@Get("upZTList")
	public String upZTList(@Param("subjectId")int subjectId,@Param("releaseAt")String releaseAt, @Param("columnsId")int columnsId, @Param("start")int start, @Param("length")int length, @Param("devicesId")String devicesId) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> news = newsDao.queryNewsBySublectId(subjectId, columnsId, start, length,releaseAt);//新闻列表
		news = formReturnList(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
		jo.put("list", news);
		
		int size = newsDao.querySizeBySublectId(subjectId, columnsId,releaseAt);
		jo.put("total", size);
		
		return "@"+jo.toString();
	}
	
	/**
	 * 专题列表数据 下拉刷新
	 * @param subjectId		主题
	 * @param columnsId		栏目
	 * @param newestId		新闻ID
	 * @param devicesId		设备ID
	 * @return
	 * @throws Exception
	 */
	@Get("downZTList")
	public String downZTList(@Param("subjectId")int subjectId, @Param("columnsId")int columnsId,@Param("releaseAt")String releaseAt, @Param("devicesId")String devicesId) throws Exception{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> subject = newsDao.querySubjectById(subjectId);//专题详情
		
		if(subject != null && subject.size() > 0){
			Map map = subject.get(0);
//			map.put("carouselImage", map.get("carouselImage")==null?"":imgPath+map.get("carouselImage").toString());//标签
			
			//缩率图
			String carouselImage = map.get("carouselImage")==null?"":map.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				//map.put("carouselImage", imgPath+carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
				//图片名称
				String fileName = carouselImages[1];
				//扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
				
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	map.put("carouselImage", imgPath+carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
                }else{
                	map.put("carouselImage", imgPath+carouselImages[0]+"/"+carouselImages[1]);//缩率图
                }
			}
			else
			{
				map.put("carouselImage", "");//缩率图
			}
			
			jo.put("subject", map);
			
			List<Map> news = newsDao.queryNewsByNewestId(subjectId, columnsId, releaseAt);//新闻列表
			news = formReturnList(news);//时间格式化，图集新闻图片提取，外链新闻，形成跳转url
			jo.put("list", news);
			
			int size = newsDao.querySizeByNewestId(subjectId, columnsId, releaseAt);
			jo.put("total", size);
		}
		else{
			jo.put("returnFlag", Constants.returnObj.returnFlag_flase);
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 时间格式化，形成跳转url，外链新闻外链，图集新闻图片提取
	 * @param news
	 * @param columnls
	 * @return
	 * @throws ParseException
	 */
	public List<Map> formReturnList(List<Map> news,List<Map> columnls) throws ParseException {
		for(Map map : news){
			
			String title = map.get("title").toString();
			
			if(title.length()>25)
			{
				map.put("title", title.substring(0, 25)+"...");
			}
			
			if(map.get("releaseAt") != null && !"".equals(map.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_SMALL_STR);
				map.put("releaseAtMinNumber", DateUtils.getBetweenMinNumber(
						DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_FULL_STR),
						DateUtils.getNowTime()));//发布时间
				map.put("releaseAt", releaseAt);
			}
			map.put("label", map.get("label")==null?"":map.get("label").toString());//标签
//			map.put("carouselImage", map.get("carouselImage")==null?"":imgPath+map.get("carouselImage").toString());//标签
			
			String columnName = "";
			for(Map cmap : columnls)
			{
				if(cmap.get("id").toString().equals(map.get("columnsId").toString()))
				{
					columnName = cmap.get("name").toString();
				}
			}
			map.put("columnName", columnName);//缩率图
			//缩率图
			String carouselImage = map.get("carouselImage")==null?"":map.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				//map.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
				//图片名称
				String fileName = carouselImages[1];
				//扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
				
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	map.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
                }else{
                	map.put("carouselImage", imgPath+carouselImages[0]+"/"+carouselImages[1]);//缩率图
                }
			}
			else
			{
				map.put("carouselImage", "");//缩率图
			}
			
			int isOuterLink = Integer.parseInt((map.get("isOuterLink").toString()));
			if(isOuterLink == Constants.flag.type1){//首先判断是否外链,是外链，直接在map中加url跳转
				map.put("url", map.get("outerLinkUrl").toString());
			}
			else{
				if (map.get("type") != null) {
					int type = Integer.parseInt(map.get("type").toString());
					if (type == NewsTypeEnum.TJXW.getValue()) {//图集新闻
						List<String> imgs = extractImg(map.get("content").toString());
						if(imgs.size() > 3){//列表最多3张图片
							imgs = imgs.subList(0, 3);
						}
						map.put("imgs", imgs);
						map.put("url", "tj?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.TWXW.getValue()) {//图文新闻
						map.put("url", "tw?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.SPXW.getValue()) {//视频新闻
						map.put("url", "sp?id=" + map.get("id").toString());
					} 
				}
				if(map.get("subjectId")!=null && !"".equals(map.get("subjectId").toString())){
					String subjectId = map.get("subjectId").toString();
					
					List<Map> reps = configDao.querySubjectById(Integer.parseInt(subjectId));
					String subjectName = reps.get(0).get("name").toString();
					map.put("subjectName", subjectName);
					
					String columnsId = map.get("columnsId").toString();
					map.put("url", "zt?subjectId=" + subjectId + "&columnsId=" + columnsId);
				}
			}
			
			map.remove("content");//去掉content 免得占流量 取content是为了获取img
		}
		
		return news;
	}
	
	/**
	 * 时间格式化，形成跳转url，外链新闻外链，图集新闻图片提取
	 * @param news
	 * @return
	 * @throws ParseException
	 */
	public List<Map> formReturnListTop(List<Map> news) throws ParseException {
		for(Map map : news){
			
			String title = map.get("title").toString();
			
			if(title.length()>25)
			{
				map.put("title", title.substring(0, 25)+"...");
			}
			
			if(map.get("releaseAt") != null && !"".equals(map.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				String releaseAtG = DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_SMALL_STR);
				map.put("releaseAtMinNumber", DateUtils.getBetweenMinNumber(releaseAt,DateUtils.getNowTime()));//发布时间
				map.put("releaseAt", releaseAtG);
			}
			map.put("label", map.get("label")==null?"":map.get("label").toString());//标签
			
			//缩率图
			String carouselImage = map.get("carouselImage")==null?"":map.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				//map.put("carouselImage", imgPath+carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
				//图片名称
				String fileName = carouselImages[1];
				//扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
				
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	map.put("carouselImage", imgPath+carouselImages[0]+"/s_"+carouselImages[1]);//缩率图
                }else{
                	map.put("carouselImage", imgPath+carouselImages[0]+"/"+carouselImages[1]);//缩率图
                }
			}
			else
			{
				map.put("carouselImage", "");//缩率图
			}
			
			int isOuterLink = Integer.parseInt((map.get("isOuterLink").toString()));
			if(isOuterLink == Constants.flag.type1){//首先判断是否外链,是外链，直接在map中加url跳转
				map.put("url", map.get("outerLinkUrl").toString());
			}
			else{
				if (map.get("type") != null) {
					int type = Integer.parseInt(map.get("type").toString());
					if (type == NewsTypeEnum.TJXW.getValue()) {//图集新闻
						List<String> imgs = extractImg(map.get("content").toString());
						if(imgs.size() > 3){//列表最多3张图片
							imgs = imgs.subList(0, 3);
						}
						map.put("imgs", imgs);
						map.put("url", "tj?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.TWXW.getValue()) {//图文新闻
						map.put("url", "tw?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.SPXW.getValue()) {//视频新闻
						map.put("url", "sp?id=" + map.get("id").toString());
					} 
				}
				if(map.get("subjectId")!=null && !"".equals(map.get("subjectId").toString())){
					String subjectId = map.get("subjectId").toString();
					
					List<Map> reps = configDao.querySubjectById(Integer.parseInt(subjectId));
					String subjectName = reps.get(0).get("name").toString();
					map.put("subjectName", subjectName);
					
					String columnsId = map.get("columnsId").toString();
					map.put("url", "zt?subjectId=" + subjectId + "&columnsId=" + columnsId);
					
				}
			}
			
			map.remove("content");//去掉content 免得占流量 取content是为了获取img
		}
		
		return news;
	}
	
	/**
	 * 时间格式化，形成跳转url，外链新闻外链，图集新闻图片提取
	 * @param news
	 * @return
	 * @throws ParseException
	 */
	public List<Map> formReturnList(List<Map> news) throws ParseException {
		for(Map map : news){
			
			String title = map.get("title").toString();
			
			if(title.length()>25)
			{
				map.put("title", title.substring(0, 25)+"...");
			}
			
			if(map.get("releaseAt") != null && !"".equals(map.get("releaseAt").toString())){//时间格式化 去掉从数据库中查出来的小数点及小数点后的0
				String releaseAt = DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_FULL_STR);
				String releaseAtG = DateUtils.formatDateStr(map.get("releaseAt").toString(), DateUtils.DATE_SMALL_STR);
				map.put("releaseAtMinNumber", DateUtils.getBetweenMinNumber(releaseAt,DateUtils.getNowTime()));//发布时间
				map.put("releaseAt", releaseAtG);
			}
			map.put("label", map.get("label")==null?"":map.get("label").toString());//标签
			
			//缩率图
			String carouselImage = map.get("carouselImage")==null?"":map.get("carouselImage").toString();
			if(carouselImage !=null && !"".equals(carouselImage))
			{
				String carouselImages[] = carouselImage.split("/");
				//map.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
				//图片名称
				String fileName = carouselImages[1];
				//扩展名(类似:.gif)
                String kzm = ReturnUUID.getFileExt(fileName);
				
                if(!imgkzm.toLowerCase().equals(kzm.toLowerCase())){
                	map.put("carouselImage", imgPath+carouselImages[0]+"/ss_"+carouselImages[1]);//缩率图
                }else{
                	map.put("carouselImage", imgPath+carouselImages[0]+"/"+carouselImages[1]);//缩率图
                }
			}
			else
			{
				map.put("carouselImage", "");//缩率图
			}
			
			int isOuterLink = Integer.parseInt((map.get("isOuterLink").toString()));
			if(isOuterLink == Constants.flag.type1){//首先判断是否外链,是外链，直接在map中加url跳转
				map.put("url", map.get("outerLinkUrl").toString());
			}
			else{
				if (map.get("type") != null) {
					int type = Integer.parseInt(map.get("type").toString());
					if (type == NewsTypeEnum.TJXW.getValue()) {//图集新闻
						List<String> imgs = extractImg(map.get("content").toString());
						if(imgs.size() > 3){//列表最多3张图片
							imgs = imgs.subList(0, 3);
						}
						map.put("imgs", imgs);
						map.put("url", "tj?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.TWXW.getValue()) {//图文新闻
						map.put("url", "tw?id=" + map.get("id").toString());
					} else if (type == NewsTypeEnum.SPXW.getValue()) {//视频新闻
						map.put("url", "sp?id=" + map.get("id").toString());
					} 
				}
				if(map.get("subjectId")!=null && !"".equals(map.get("subjectId").toString())){
					String subjectId = map.get("subjectId").toString();
					
					List<Map> reps = configDao.querySubjectById(Integer.parseInt(subjectId));
					String subjectName = reps.get(0).get("name").toString();
					map.put("subjectName", subjectName);
					
					String columnsId = map.get("columnsId").toString();
					map.put("url", "zt?subjectId=" + subjectId + "&columnsId=" + columnsId);
					
				}
			}
			
			map.remove("content");//去掉content 免得占流量 取content是为了获取img
		}
		
		return news;
	}
	
	private List<String> extractImg(String html) {
		List<String> imgs = new ArrayList<String>();
		
		Document doc = Jsoup.parse(html);
        Elements ele = doc.getElementsByTag("img");
        Elements elep = doc.getElementsByTag("p");
        
        for(Element e : ele){
        	String src = e.attr("src");
        	if(src !=null && src.length() > 0){
				String carouselImages[] = src.split("/");
				String carouselImage = imgPath+carouselImages[carouselImages.length-2]+"/s"+carouselImages[carouselImages.length-1];//缩率图
        		imgs.add(carouselImage);
        	}
        }
        
        return imgs;
	}
	
	private Map extractImgAndText(String html) {
		Map returnlis = new HashMap();
		List<String> imgs = new ArrayList<String>();
		List<String> texts = new ArrayList<String>();
		
		Document doc = Jsoup.parse(html);
        Elements ele = doc.getElementsByTag("img");
        Elements elep = doc.getElementsByTag("p");
        
        for(Element e : elep){
        	String src = e.text();
        	if(src !=null && src.length() > 0){
        		texts.add(src);
        	}
        }
        
        for(Element e : ele){
        	String src = e.attr("src");
        	if(src !=null && src.length() > 0){
        		imgs.add(src);
        	}
        }
        
        Map repsImgs = new HashMap();
        returnlis.put("imgs", imgs);
        Map repsDocs = new HashMap();
        returnlis.put("texts", texts);
        
        return returnlis;
	}

	/*********************************************************
	 * 点赞新闻
	 * @param devicesId
	 * @param feedbackId
	 * @return
	 */
	@Get("createPraise")
	public String createPraise(@Param("devicesId") String devicesId,@Param("newsId") int newsId)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			String createUser = apuser.get(0).get("name")==null?apuser.get(0).get("id").toString():apuser.get(0).get("name").toString();
			
			int weixinuuid = Integer.parseInt(apuser.get(0).get("weixinuuid").toString());
			int qquuid = Integer.parseInt(apuser.get(0).get("qquuid").toString());
			int weibouuid = Integer.parseInt(apuser.get(0).get("weibouuid").toString());
			int accountlogin = Integer.parseInt(apuser.get(0).get("accountlogin").toString());
			
			String wxid = apuser.get(0).get("wxid")==null?"":apuser.get(0).get("wxid").toString();
			String qqid = apuser.get(0).get("qqid")==null?"":apuser.get(0).get("qqid").toString();
			String sinawbid = apuser.get(0).get("sinawbid")==null?"":apuser.get(0).get("sinawbid").toString();
			String account = apuser.get(0).get("account")==null?"":apuser.get(0).get("account").toString();
			
			//回复用户名称修改为第三方登录ID
			String userName = devicesId;
			if(weixinuuid==1){
				//微信登录
				userName=wxid;
			}else if(qquuid==1){
				//QQ登录
				userName=qqid;
			}else if(weibouuid==1){
				//微博登录
				userName=sinawbid;
			}else if(accountlogin==1){
				//微博登录
				userName=account;
			}
			
			int size = newsOpDao.queryPraiseSize(newsId, userName);
			
			if(size==0)
				//没有点赞记录
			{
				int userId = Integer.parseInt(apuser.get(0).get("id").toString());
				List<Map> repNews = newsDao.queryById(newsId);
				Map rep = new HashMap();
				rep.put("newsId", newsId);
				rep.put("newsTitle", repNews.get(0).get("title")==null?"":repNews.get(0).get("title").toString());
				rep.put("userId", userName);
				rep.put("userName", createUser);
				rep.put("createUser", userName);
				rep.put("updateUser", userName);
				//点赞记录
				newsOpDao.createPraise(rep);
				printUserActionLog(ActionEnum.PRAISE.getValue(), userId, newsId);
				newsOpDao.addPraiseById(newsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_1);
			}
			else
				//存在点赞记录
			{
				//取消点赞记录
				newsOpDao.delPraise(newsId, userName);
				newsOpDao.subPraiseById(newsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_2);
			}
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 收藏新闻
	 * @param devicesId
	 * @param feedbackId
	 * @return
	 */
	@Get("createCollect")
	public String createCollect(@Param("devicesId") String devicesId,@Param("newsId") int newsId)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			int weixinuuid = Integer.parseInt(apuser.get(0).get("weixinuuid").toString());
			int qquuid = Integer.parseInt(apuser.get(0).get("qquuid").toString());
			int weibouuid = Integer.parseInt(apuser.get(0).get("weibouuid").toString());
			int accountlogin = Integer.parseInt(apuser.get(0).get("accountlogin").toString());
			
			String wxid = apuser.get(0).get("wxid")==null?"":apuser.get(0).get("wxid").toString();
			String qqid = apuser.get(0).get("qqid")==null?"":apuser.get(0).get("qqid").toString();
			String sinawbid = apuser.get(0).get("sinawbid")==null?"":apuser.get(0).get("sinawbid").toString();
			String account = apuser.get(0).get("account")==null?"":apuser.get(0).get("account").toString();
			
			//回复用户名称修改为第三方登录ID
			String userName = devicesId;
			if(weixinuuid==1){
				//微信登录
				userName=wxid;
			}else if(qquuid==1){
				//QQ登录
				userName=qqid;
			}else if(weibouuid==1){
				//微博登录
				userName=sinawbid;
			}else if(accountlogin==1){
				//微博登录
				userName=account;
			}
			
			int size = newsOpDao.queryCollectSize(newsId, userName);
			
			if(size==0)
				//没有收藏记录
			{
				int userId = Integer.parseInt(apuser.get(0).get("id").toString());
				List<Map> repNews = newsDao.queryById(newsId);
				Map rep = new HashMap();
				rep.put("newsId", newsId);
				rep.put("newsTitle", repNews.get(0).get("title")==null?"":repNews.get(0).get("title").toString());
				rep.put("userId", userName);
				rep.put("userName", userName);
				rep.put("createUser", userName);
				rep.put("updateUser", userName);
				//收藏记录
				newsOpDao.createCollect(rep);
				printUserActionLog(ActionEnum.COLLECT.getValue(), userId, newsId);
				newsOpDao.addCollectById(newsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_1);
			}
			else
				//存在收藏记录
			{
				//取消收藏记录
				newsOpDao.delCollect(newsId, userName);
				newsOpDao.subCollectById(newsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_2);
			}
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 转发新闻
	 * @param devicesId
	 * @param feedbackId
	 * @return
	 */
	@Get("createForward")
	public String createForward(@Param("devicesId") String devicesId,@Param("newsId") int newsId)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			String createUser = apuser.get(0).get("name")==null?apuser.get(0).get("id").toString():apuser.get(0).get("name").toString();
			int userId = Integer.parseInt(apuser.get(0).get("id").toString());
			int size = newsOpDao.queryForwardSize(newsId, userId);
			if(size==0)
				//没有转发记录
			{
				List<Map> repNews = newsDao.queryById(newsId);
				/*Map rep = new HashMap();
				rep.put("newsId", newsId);
				rep.put("newsTitle", repNews.get(0).get("title")==null?"":repNews.get(0).get("title").toString());
				rep.put("userId", userId);
				rep.put("userName", createUser);
				rep.put("createUser", userId);
				rep.put("updateUser", userId);
				//转发记录
				newsOpDao.createForward(rep);*/
				printUserActionLog(ActionEnum.FORWARD.getValue(), userId, newsId);
				newsOpDao.addForwardById(newsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_1);
			}
			else
				//存在转发记录
			{
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_2);
			}
		}
		
		return "@"+jo.toString();
	}
	
	/*******************************************新闻评论
	/**
	 * 查询评论列表
	 * @param newsId	新闻ID
	 * @param start		起始
	 * @param length	尺寸
	 * @param order		排序号	0：stickAt DESC,createAt DESC；1：praiseNum DESC
	 * @param createAt	创建时间
	 * @return
	 */
	@Get("getCommentsList")
	public String queryCommentsList(@Param("newsId") int newsId,@Param("start") int start,@Param("length") int length,
			@Param("order") int order,@Param("createAt") String createAt,@Param("devicesId") String devicesId) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> commentsls = newsOpDao.queryComments(newsId, start, length,order,createAt);
		
		List<Map> reps = returnCommentsls(commentsls,devicesId);
		int size = newsOpDao.queryCommentsSize(newsId,createAt);
		
		jo.put("commentsls", reps);
		jo.put("total", size);
		return "@"+jo.toString();	
	}
	
	/**
	 * 提交新闻评论
	 * @param devicesId		设备ID
	 * @param newsId		新闻ID
	 * @param reply			回复内容
	 * @param isAnonymous	是否管理员	0：不是	1：是
	 * @param commentsId	评论ID
	 * @return
	 */
	@Get("createComments")
	public String createComments(@Param("devicesId") String devicesId,@Param("newsId") int newsId,
			@Param("reply") String reply,@Param("isAnonymous") int isAnonymous,@Param("commentsId") int commentsId)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			boolean flag = returnWordFlag(reply);
			if(flag){
				//true
				//禁言
				jo.put("appUserInfoState", AppUserInfoStateEnum.MG.getValue());
				jo.put("returnMsg", "抱歉，您的评论未能通过!");
				return "@"+jo.toString();
			}
			
			int state = Integer.parseInt(apuser.get(0).get("state").toString());
			if(AppUserInfoStateEnum.JY.getValue()==state){
				//禁言
				jo.put("appUserInfoState", AppUserInfoStateEnum.JY.getValue());
				jo.put("returnMsg", "您已被禁言!");
			}else{
				jo.put("appUserInfoState", AppUserInfoStateEnum.ZC.getValue());
				
				List<Map> news = newsDao.queryNewsSize(newsId);
				
				if(news.size()>0)
				{
					int isComments = Integer.parseInt(news.get(0).get("isComments").toString());
					
					if(isComments==0)
					{
						//禁言
						jo.put("appUserInfoState", AppUserInfoStateEnum.XWJY.getValue());
						jo.put("returnMsg", "新闻已被禁言!");
						return "@"+jo.toString();
					}
					
					//回复用户ID
					int replyUserId = Integer.parseInt(apuser.get(0).get("id").toString());

					int weixinuuid = Integer.parseInt(apuser.get(0).get("weixinuuid").toString());
					int qquuid = Integer.parseInt(apuser.get(0).get("qquuid").toString());
					int weibouuid = Integer.parseInt(apuser.get(0).get("weibouuid").toString());
					int accountlogin = Integer.parseInt(apuser.get(0).get("accountlogin").toString());
					
					String wxid = apuser.get(0).get("wxid")==null?"":apuser.get(0).get("wxid").toString();
					String qqid = apuser.get(0).get("qqid")==null?"":apuser.get(0).get("qqid").toString();
					String sinawbid = apuser.get(0).get("sinawbid")==null?"":apuser.get(0).get("sinawbid").toString();
					String account = apuser.get(0).get("account")==null?"":apuser.get(0).get("account").toString();
					
					//回复用户名称修改为第三方登录ID
					String replyName = devicesId;
					if(weixinuuid==1){
						//微信登录
						replyName=wxid;
					}else if(qquuid==1){
						//QQ登录
						replyName=qqid;
					}else if(weibouuid==1){
						//微博登录
						replyName=sinawbid;
					}else if(accountlogin==1){
						//微博登录
						replyName=account;
					}
					
					if(commentsId==0){
						Map rep = new HashMap();
						rep.put("newsId", newsId);
						rep.put("contents", news.get(0).get("columnsId").toString());
						rep.put("newsTitle", news.get(0).get("title").toString());
						rep.put("isAnonymous", isAnonymous);
						rep.put("reply", reply);
						rep.put("replyUserId", replyUserId);
						rep.put("replyName", replyName);
						rep.put("createUser", replyUserId);
						
						newsOpDao.createComments(rep);
					}else{
						Map rep = new HashMap();
						rep.put("newsId", newsId);
						rep.put("contents", news.get(0).get("columnsId").toString());
						rep.put("newsTitle", news.get(0).get("title").toString());
						rep.put("isAnonymous", isAnonymous);
						rep.put("reply", reply);
						rep.put("replyUserId", replyUserId);
						rep.put("replyName", replyName);
						rep.put("createUser", replyUserId);
						rep.put("updateUser", replyUserId);
						
						List<Map> reps = newsOpDao.queryCommentsById(commentsId);
						if(reps!=null && reps.size()>0)
						{
							rep.put("editReplyUserId", reps.get(0).get("replyName"));
							rep.put("editReply", reps.get(0).get("reply"));
						}
						
						newsOpDao.createCommentsByEditReply(rep);
					}
					
				}
			}
		}
		
		return "@"+jo.toString();
	}
	
	/**
	 * 删除评论
	 * @param plid
	 * @return
	 */
	@Get("delPl")
	public String delPl(@Param("id") int id,@Param("newsId") int newsId) {
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		newsOpDao.delCommentsById(id);
		newsOpDao.subCommentsById(newsId);
		return "@"+jo.toString();	
	}
	
	/**
	 * 新闻评论点赞
	 * @param devicesId
	 * @param newsId
	 * @return
	 */
	@Get("createCommentsPraise")
	public String createCommentsPraise(@Param("devicesId") String devicesId,@Param("newsId") int newsId,
			@Param("commentsId") int commentsId)
	{
		JSONObject jo = new JSONObject();
		jo.put("returnFlag", Constants.returnObj.returnFlag_true);
		
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		if(apuser.size()>0)
		{
			String createUser = apuser.get(0).get("name")==null?apuser.get(0).get("id").toString():apuser.get(0).get("name").toString();
			int userId = Integer.parseInt(apuser.get(0).get("id").toString());
			int size = newsOpDao.queryCommentsPraiseSize(newsId, userId, commentsId);
			if(size==0)
				//没有评论点赞记录
			{
				Map rep = new HashMap();
				
				rep.put("newsId", newsId);
				rep.put("commentsId", commentsId);
				rep.put("userId", userId);
				rep.put("userName", createUser);
				rep.put("createUser", userId);
				rep.put("updateUser", userId);
				//评论点赞记录
				newsOpDao.createCommentsPraise(rep);
				newsOpDao.addCommentsPraiseById(commentsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_1);
			}
			else
				//存在评论点赞记录
			{
				//取消评论点赞记录
				newsOpDao.delCommentsPraise(newsId, userId, commentsId);
				newsOpDao.subCommentsPraiseById(commentsId);
				jo.put("returnFlagCode", Constants.returnObj.returnPraise.code_2);
			}
		}
		
		return "@"+jo.toString();
	}
	
	@Get("share")//预览
	public String share(Model model,@Param("newsId") int newsId,HttpServletRequest request){
		List<Map> news = newsDao.queryById(newsId);
		
		if(news!=null && news.size()>0){
			model.add("id", news.get(0).get("id").toString());
			model.add("columnsId", news.get(0).get("columnsId").toString());
			model.add("title", news.get(0).get("title").toString());
			model.add("abstractInfor", news.get(0).get("abstractInfor")==null?"":news.get(0).get("abstractInfor").toString());
			//视频地址
			model.add("vedioUrl", (news.get(0).get("vedioUrl")==null || "".equals(news.get(0).get("vedioUrl")))?"":videoPath+news.get(0).get("vedioUrl").toString());
			model.add("carouselImage", news.get(0).get("carouselImage")==null?"":imgPath+news.get(0).get("carouselImage").toString());
			model.add("content", news.get(0).get("content")==null?"":news.get(0).get("content").toString());
			model.add("source", news.get(0).get("source")==null?"":news.get(0).get("source").toString());
			model.add("author", news.get(0).get("author")==null?"":news.get(0).get("author").toString());
			String releaseAt = DateUtils.getMinusTime(news.get(0).get("releaseAt").toString(), DateUtils.DATE_FULL_STR, 0);
			model.add("releaseAt", releaseAt);
			model.add("isOuterLink", news.get(0).get("isOuterLink")==null?0:news.get(0).get("isOuterLink").toString());
			model.add("outerLinkUrl", news.get(0).get("outerLinkUrl")==null?"":news.get(0).get("outerLinkUrl").toString());
			
			//投票
			Map obj = news.get(0);
			if(null != obj.get("voteId") && !"".equals(obj.get("voteId").toString())){
				int voteId = Integer.parseInt(obj.get("voteId").toString());
				if(voteId > -1){
					List<Map> subjects = newsDao.getSubjByVoteId(voteId);
					
					if(null != subjects && subjects.size() > 0){
						for(Map subject : subjects){
							int subId = Integer.parseInt(subject.get("id").toString());
							List<Map> items = newsDao.getItmBySubId(subId);
							int total = 0;
							if(null != items && items.size() > 0){
								for(Map item : items){
									int count = Integer.parseInt(item.get("count").toString());
									total += count;
								}
								
								if(total == 0){
									for(Map item : items){
										item.put("percent", "0%");
									}
								}
								else{
									int headPercents = 0;
									
									for(int i = 0; i < items.size(); i++){
										int count = Integer.parseInt(items.get(i).get("count").toString());
										int percent = Math.round((float)count/total * 100);
										if( i ==  items.size() -1){
											percent = 100 - headPercents;
										}
										headPercents += percent;
										items.get(i).put("percent", percent + "%");
									}
								}
								
								subject.put("items", items);
							}
						}
					}
					
					model.add("Vote", subjects);
				}
			}
		}
		else
		{
			return "/views/empty.vm";
		}
		
		return "/views/share.vm";
	}
	
	/**
	 * 列表重构=》List<Map>
	 * @param answersls
	 * @return
	 */
	private List<Map> returnCommentsls(List<Map> commentsls,String devicesId)
	{
		List<Map> apuser = appuserDao.queryAppUserByDevicesId(devicesId);
		
		int userId = Integer.parseInt(apuser.get(0).get("id").toString());
		
		List<Map> reps = new ArrayList<Map>();
		for(Map rep :commentsls)
		{
			Map obj = new HashMap();
			obj.put("id", rep.get("id"));
			//是否管理员	0：不是	1：是
			int isAnonymous = Integer.parseInt(rep.get("isAnonymous").toString());
			String editReplyUserId = rep.get("editReplyUserId")==null?"":rep.get("editReplyUserId").toString();
			String editReply = rep.get("editReply")==null?"":rep.get("editReply").toString();
			String replyName = rep.get("replyName")==null?"":rep.get("replyName").toString();
			obj.put("replyUserId", replyName);
			if(!"".equals(replyName))
			{
				List<Map> appuser = appuserDao.queryAppDevicesAndUid(rep.get("replyName").toString());
				if(appuser!=null && appuser.size()>0)
				{
					obj.put("replyUserPhoto", appuser.get(0).get("photo")==null?"":appuser.get(0).get("photo").toString());
					obj.put("replyUserName", appuser.get(0).get("name")==null?"":Base64.getFromBase64(appuser.get(0).get("name").toString()));
				}
				else
				{
					if("0".equals(replyName))
					{
						obj.put("replyUserPhoto", "");
						obj.put("replyUserName", "小编");
					}
					else
					{
						obj.put("replyUserPhoto", "");
						obj.put("replyUserName", "游客");
					}
				}
			}
			
			obj.put("createAt", DateUtils.getBetweenMinNumber(rep.get("createAt").toString(),DateUtils.getNowTime()));//发布时间
			
			obj.put("newsId", rep.get("newsId"));
			//回复内容
			obj.put("reply", rep.get("reply"));
			
			//是否管理员	0：不是	1：是
			obj.put("isAnonymous",isAnonymous);
			if(!"".equals(editReply))
			{
				//用户ID
				obj.put("editReplyUserId", editReplyUserId);
				//用户回复内容
				obj.put("editReply", editReply);
				
				List<Map> appuser = appuserDao.queryAppDevicesAndUid(editReplyUserId);
				if(appuser!=null && appuser.size()>0)
				{
					obj.put("editReplyUserPhoto", appuser.get(0).get("photo")==null?"":appuser.get(0).get("photo").toString());
					obj.put("editReplyUserName", appuser.get(0).get("name")==null?"游客":Base64.getFromBase64(appuser.get(0).get("name").toString()));
				}
				else
				{
					if("0".equals(editReplyUserId))
					{
						obj.put("editReplyUserPhoto", "");
						obj.put("editReplyUserName", "小编");
					}
					else
					{
						obj.put("editReplyUserPhoto", "");
						obj.put("editReplyUserName", "游客");
					}
					
				}
			}
			
			obj.put("praiseNum", rep.get("praiseNum")==null?0:rep.get("praiseNum").toString());
			
			int praisePoint = newsOpDao.queryCommentsPraiseSize(Integer.parseInt(rep.get("newsId").toString()), 
					userId,Integer.parseInt(rep.get("id").toString()));
			
			//是否存在点赞
			obj.put("praiseOp", praisePoint);
			
			reps.add(obj);
		}
		
		return reps;
	}
	
	/**
	 * 返回文字是否通过敏感词库
	 * @param contants
	 * @return	true:false
	 */
	private boolean returnWordFlag(String contants)
	{
		List<Map> words = configDao.queryLexiconAll();
		
		if(words.size()>0){
			for(Map rep:words){
				//查询敏感词
				String word = rep.get("name").toString();
				if(contants.indexOf(word)==-1){
					//判断不存在
					continue;
				}else{
					return true;
				}
			}
		}
		
		return false;
	}
	
	/**
	 * 返回用户是否操作：点赞与收藏
	 * @param devicesId
	 * @param newsId
	 * @return
	 */
	private Map returnMyOp(String userName,int newsId)
	{
		int praisePoint = newsOpDao.queryPraiseSize(newsId, userName);
		int collectPoint = newsOpDao.queryCollectSize(newsId, userName);
		
		Map rep = new HashMap();
		rep.put("praiseOp", praisePoint);
		rep.put("collectOp", collectPoint);
		return rep;
	}
	
	/**
	 * 打印用户行为记录
	 * @param action	动作
	 * @param userId	用户ID
	 * @param newsId	新闻ID
	 */
	private void printUserActionLog(int action,int userId,int newsId)
	{
		/**
		 * Action
		 * 1:view 
		 * 2:praise
		 * 3:collect
		 * 4:forward
		 * userId|action|newsId
		 */
		logger.warn(userId+"|"+action+"|"+newsId);
	}
}
